# 计算器

from tkinter import *

class Calculator:
  def __init__(self):
    # 创建根窗口，并且保存到类的成员属性中
    self.root = Tk()
    self.root.geometry("300x500")
    self.root.title("小溪流的计算器")

    # 创建calcNum变量记录计算面板要显示的值
    self.calcNum = StringVar()
    self.calcNum.set(0)

    # 记录运算符号的按下状态，如果True则是按下，反之没有被按下
    self.isPressOn = False

    # 列表记录运算数字和运算符号
    self.lists = []

    # 计算器主界面布局
    self.show()
    self.root.mainloop()

  def show(self):
    # 计算面板布局
    frameTop = Frame(self.root, width=300, height=5)
    frameTop.pack(padx=10, pady=1)
    labelTop = Label(frameTop, textvariable = self.calcNum, width=300, height=3, font=("黑体", 16, "bold"), anchor="e")
    labelTop.pack()

    # 按钮区布局
    frameButton = Frame(self.root, width=300, height=180)
    frameButton.pack(padx=5, pady=1)

    # 第一行
    b_1 = Button(frameButton, text="MC", width=6, height=3).grid(row=0, column=0)
    b_2 = Button(frameButton, text="MR", width=6, height=3).grid(row=0, column=1)
    b_3 = Button(frameButton, text="MS", width=6, height=3).grid(row=0, column=2)
    b_4 = Button(frameButton, text="M+", width=6, height=3).grid(row=0, column=3)
    b_5 = Button(frameButton, text="M-", width=6, height=3).grid(row=0, column=4)

    # 第二行
    b_6 = Button(frameButton, text="←", width=6, height=3).grid(row=1, column=0)
    b_7 = Button(frameButton, text="CE", width=6, height=3).grid(row=1, column=1)
    b_8 = Button(frameButton, text="C", width=6, height=3).grid(row=1, column=2)
    b_9 = Button(frameButton, text="±", width=6, height=3).grid(row=1, column=3)
    b_10 = Button(frameButton, text="√", width=6, height=3).grid(row=1, column=4)

    # 第三行
    b_11 = Button(frameButton, text="7", width=6, height=3, command = lambda: self.pressNum('7')).grid(row=2, column=0)
    b_12 = Button(frameButton, text="8", width=6, height=3, command = lambda: self.pressNum('8')).grid(row=2, column=1)
    b_13 = Button(frameButton, text="9", width=6, height=3, command = lambda: self.pressNum('9')).grid(row=2, column=2)
    b_14 = Button(frameButton, text="/", width=6, height=3, command = lambda: self.pressSign('/')).grid(row=2, column=3)
    b_15 = Button(frameButton, text="%", width=6, height=3).grid(row=2, column=4)

    # 第四行
    b_16 = Button(frameButton, text="4", width=6, height=3, command = lambda: self.pressNum('4')).grid(row=3, column=0)
    b_17 = Button(frameButton, text="5", width=6, height=3, command = lambda: self.pressNum('5')).grid(row=3, column=1)
    b_18 = Button(frameButton, text="6", width=6, height=3, command = lambda: self.pressNum('6')).grid(row=3, column=2)
    b_19 = Button(frameButton, text="*", width=6, height=3, command = lambda: self.pressSign('*')).grid(row=3, column=3)
    b_20 = Button(frameButton, text="1/x", width=6, height=3).grid(row=3, column=4)

    # 第五行
    b_21 = Button(frameButton, text="1", width=6, height=3, command = lambda: self.pressNum('1')).grid(row=4, column=0)
    b_22 = Button(frameButton, text="2", width=6, height=3, command = lambda: self.pressNum('2')).grid(row=4, column=1)
    b_23 = Button(frameButton, text="3", width=6, height=3, command = lambda: self.pressNum('3')).grid(row=4, column=2)
    b_24 = Button(frameButton, text="-", width=6, height=3, command = lambda: self.pressSign('-')).grid(row=4, column=3)
    b_25 = Button(frameButton, text="=", width=6, height=7, command = lambda: self.calcRes()).grid(row=4, column=4, rowspan=2)

    # 第六行
    b_26 = Button(frameButton, text="0", width=13, height=3, command = lambda: self.pressNum('0')).grid(row=5, column=0, columnspan=2)
    b_27 = Button(frameButton, text=".", width=6, height=3, command = lambda: self.pressNum('.')).grid(row=5, column=2)
    b_28 = Button(frameButton, text="+", width=6, height=3, command = lambda: self.pressSign('+')).grid(row=5, column=3)


  def pressNum(self, num):
    # 如果按下了运算符号，则将展示屏的数字设置为0
    if self.isPressOn == True:
      self.calcNum.set(0)
      # 重置运算符号的状态
      self.isPressOn = False

    if num == ".":
      num = "0."

    oldNum = self.calcNum.get()

    if oldNum == "0":
      self.calcNum.set(num)
    else:
      newNum = oldNum + num
      self.calcNum.set(newNum)

  # 按下运算符号
  def pressSign(self, sign):
    # 设置运算符号的按下状态为True
    self.isPressOn = True
    # 将数字和按下的运算符号记录下来
    num = self.calcNum.get()
    self.lists.append(num)
    self.lists.append(sign)

  # 计算结果
  def calcRes(self):
    # 获取计算面板上的数字
    calcNum = self.calcNum.get()
    # 将计算面板上的数字存入列表
    self.lists.append(calcNum)
    # 将列表转化为算术表达式
    calcStr = ''.join(self.lists)
    # 使用eval执行算术表达式
    res = eval(calcStr)
    # 将运算结果显示在计算面板中
    self.calcNum.set(res)
    if self.lists != 0:
      self.isPressOn = True
    # 清空列表
    self.lists.clear()

# 实例化对象
calculator = Calculator()
